<template>
    <el-container>
        <el-header>
            <el-page-header :content="data.name"></el-page-header>
            <div class="do">
                <el-button type="primary" @click="exportJson">export JSON</el-button>
                <el-button type="primary" @click="test">test</el-button>
            </div>
        </el-header>
        <el-main>
            <sc-workflow ref="flow" v-model="data.nodeConfig"></sc-workflow>
        </el-main>
    </el-container>
</template>

<script>

    import scWorkflow from '@/components/scWorkflow'

    export default {
        name: 'workflow',
        components: {
            scWorkflow
        },
        data() {
            return {
                data: {
                    "id": 1,
                    "name": "流程设计",
                    "nodeConfig": {
                        "nodeName": "触发器",
                        "type": "trigger",
                        "nodeData": {
                            app:{
                                name:"webhook",
                            },
                        },
                        "childNode": {
                            "nodeName": "",
                            "type": "if",
                            "conditionMode": 1,
                            "ifNode":{
                                "childNode": {
                                    "nodeName": "咨询",
                                    "type": "func",
                                }
                            },
                            "inputs":{
                                "conditionList": [
                                    [
                                        {
                                            "label": "请假天数",
                                            "field": "day",
                                            "operator": ">",
                                            "value": "7"
                                        }
                                    ],
                                    [
                                        {
                                            "label": "请假天数",
                                            "field": "day",
                                            "operator": ">",
                                            "value": "7"
                                        }
                                    ]
                                ],
                            },
                            "childNode": {
                                "nodeName": "",
                                "type": "switch",
                                "conditionNodes": [{
                                        "nodeName": "长期",
                                        "type": "case",
                                        "priorityLevel": 1,
                                        "conditionMode": 1,
                                        "conditionList": [{
                                            "label": "请假天数",
                                            "field": "day",
                                            "operator": ">",
                                            "value": "7"
                                        }],
                                        "childNode": {
                                            "nodeName": "钉钉机器人",
                                            "type": "func",
                                            "appId":"dingtalk.js",
                                            "appName":"钉钉机器人",
                                            "eventName":"发送钉钉消息",
                                            "properties":[],
                                        }
                                    },
                                    {
                                        "nodeName": "短期",
                                        "type": "case",
                                        "priorityLevel": 2,
                                        "conditionMode": 1,
                                        "conditionList": [],
                                        "childNode": {
                                            "nodeName": "钉钉机器人",
                                            "type": "func",
                                            "childNode": {
                                                "nodeName": "",
                                                "type": "switch",
                                                "conditionNodes": [{
                                                        "nodeName": "长期",
                                                        "type": "case",
                                                        "priorityLevel": 1,
                                                        "conditionMode": 1,
                                                        "conditionList": [{
                                                            "label": "请假天数",
                                                            "field": "day",
                                                            "operator": ">",
                                                            "value": "7"
                                                        }],
                                                        "childNode": {
                                                            "nodeName": "钉钉机器人",
                                                            "type": "func",
                                                        }
                                                    },
                                                    {
                                                        "nodeName": "短期",
                                                        "type": "case",
                                                        "priorityLevel": 2,
                                                        "conditionMode": 1,
                                                        "conditionList": [],
                                                        "childNode": {
                                                            "nodeName": "钉钉机器人",
                                                            "type": "func",
                                                        }
                                                    }
                                                ]
                                            }
                                        }
                                    }
                                ],
                                "childNode": {
                                   "nodeName": "咨询",
                                   "type": "func",
                                }
                            }

                        }
                    }
                }
            }
        },
        mounted() {

        },
        methods: {
            test(){
                this.$refs.flow.updateIndex()
                this.$refs.flow.forNode(this.data.nodeConfig,null,(item)=>{
                    
                    var nextId = ""
                    var temp = item
                    var i = 0

                    if(item.type){

                        if(!item.childNode || (item.childNode && Object.keys(item.childNode).length==0)) 
                        {
                            
                            try{
                                for (i = 0; i < 100; i++) {
                                    temp = temp.parent
                                    if(temp && temp.childNode){
                                        if(temp.childNode.index>item.index){
                                            if(temp.childNode.index){
                                                nextId = temp.childNode.index;
                                                break;
                                            }
                                        }
                                    }
                                    if(temp && temp.type=="for"){
                                        nextId = temp.index;
                                        break;
                                    }
                                }
                            }catch(e){
                                console.log("test",e)
                            }

                            if(!nextId){

                                temp = item

                                try{
                                    for (i = 0; i < 100; i++) {
                                        temp = temp.parent
                                        if(temp && temp.childNode){
                                            if(temp.childNode.conditionNodes && temp.childNode.conditionNodes[0].index > item.index){
                                                nextId = temp.childNode.conditionNodes[0].index;
                                                break;
                                            }
                                        }
                                    }
                                }catch(e){
                                    console.log("test",e)
                                }

                            }

                            if(nextId){
                                item.id = item.index + "-" + nextId
                            }

                        }

                    }
                })

            },
            exportJson() {
                var nodes = [];
                var edges = [];
                var i = 1;
                this.$refs.flow.forNode(this.data.nodeConfig,null,(item,parent)=>{
                    item.parent = parent
                    if(item.type != 'stop'){
                        item.id = item.type == 'switch' ? item.parent.id : i++;
                        if(item.type != 'switch' && item.type != 'stop'){
                            nodes.push(item)
                        }
                    }
                })
                this.$refs.flow.forNode(this.data.nodeConfig,null,(item)=>{
                    if(!item.childNode && !item.conditionNodes){
                        var nextId = ""
                        try{
                            var temp = item
                            for (var i = 0; i < 100; i++) {
                                temp = temp.parent
                                if(temp && temp.childNode && temp.childNode.id>item.id){
                                    if(temp.childNode.id){
                                        nextId = temp.childNode.id;
                                        break;
                                    }
                                }
                            }
                        }catch(e){
                            console.log("test",e)
                        }

                        if(nextId){
                            edges.push({from:item.id,to:nextId})
                            item.nodeName = item.id + "-" + nextId
                        }else{
                            item.nodeName = item.id
                        }
                    }else{
                        item.nodeName = item.id
                    }
                })
                this.$refs.flow.forNode(this.data.nodeConfig,null,(item,parent)=>{
                    if(parent){
                        if(parent.id != item.id && item.id && parent.id){
                            if(parent.type!='switch' || item.type!='func'){
                                edges.push({from:parent.id,to:item.id})
                            }
                        }
                    }
                })
                console.error("nodes & edges: ",nodes,edges)
                this.$message("返回值请查看F12控制台console.log()")
                console.log(this.data)
            }
        }
    }
</script>

<style></style>
